#include <stdafx.h>
#include "rawvolgrid.h"
#include <stdio.h>

RawVolGrid::RawVolGrid(void)
{
	m_pData = NULL;
	m_dataX = m_dataY = m_dataZ = 0;

	m_voxelSize = (size_t)0;
}

RawVolGrid::~RawVolGrid(void)
{
	if(m_pData)
	{
		delete [] m_pData;
	}
	m_pData = NULL;
}

bool RawVolGrid::ReadFromFile(const char * szFileName, unsigned int xVoxels, unsigned int yVoxels, unsigned int zVoxels, size_t voxelsSize)
{
	m_dataX = xVoxels; m_dataY = yVoxels; m_dataZ = zVoxels;

	m_voxelSize = voxelsSize;

	if(m_pData)
	{
		delete [] m_pData;
	}

	m_pData = new unsigned char[m_dataX*m_dataY*m_dataZ*voxelsSize];

	// fread((float *) data, TDIM*ZDIM*XDIM*YDIM*sizeof(float),1, fp);

	FILE * fiIn = fopen(szFileName, "rb");

	size_t numBytesToRead = xVoxels*yVoxels*zVoxels*voxelsSize;
	int c = 0;
	size_t bytesRead = fread(m_pData, 1, numBytesToRead, fiIn);
	fclose(fiIn);

	if(bytesRead < numBytesToRead)
	{
		delete [] m_pData;
		m_pData = NULL;
		return false;
	}

	return true;
}
